package com.moose.operator.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author taohua
 */
public class MapperUtils {

  private final static ObjectMapper OBJECT_MAPPER = new ObjectMapper();

  public static ObjectMapper getInstance() {
    return OBJECT_MAPPER;
  }

  /**
   * 转换为 JSON 字符串
   *
   * @throws Exception
   */
  public static String obj2json(Object obj) throws Exception {
    return OBJECT_MAPPER.writeValueAsString(obj);
  }

  /**
   * 转换为 JSON 字符串，忽略空值
   *
   * @throws Exception
   */
  public static String obj2jsonIgnoreNull(Object obj) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    return mapper.writeValueAsString(obj);
  }

  /**
   * 转换为 JavaBean
   *
   * @throws Exception
   */
  public static <T> T json2pojo(String jsonString, Class<T> clazz) throws Exception {
    OBJECT_MAPPER.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
    return OBJECT_MAPPER.readValue(jsonString, clazz);
  }

  /**
   * 字符串转换为 Map<String, Object>
   *
   * @throws Exception
   */
  public static <T> Map<String, Object> json2map(String jsonString) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    return mapper.readValue(jsonString, Map.class);
  }

  /**
   * 字符串转换为 Map<String, T>
   */
  public static <T> Map<String, T> json2map(String jsonString, Class<T> clazz) throws Exception {
    Map<String, T> map = OBJECT_MAPPER.readValue(jsonString, new TypeReference<Map<String, T>>() {
    });
    Map<String, T> result = new HashMap<String, T>(16);
    for (Map.Entry<String, T> entry : map.entrySet()) {
      result.put(entry.getKey(), map2pojo(entry.getValue(), clazz));
    }
    return result;
  }

  /**
   * 深度转换 JSON 成 Map
   */
  public static Map<String, Object> json2mapDeeply(String json) throws Exception {
    return json2MapRecursion(json, OBJECT_MAPPER);
  }

  /**
   * 把 JSON 解析成 List，如果 List 内部的元素存在 jsonString，继续解析
   *
   * @param mapper 解析工具
   * @throws Exception
   */
  private static List<Object> json2ListRecursion(String json, ObjectMapper mapper)
      throws Exception {
    if (json == null) {
      return null;
    }

    List<Object> list = mapper.readValue(json, List.class);

    for (Object obj : list) {
      if (obj != null && obj instanceof String) {
        String str = (String) obj;
        if (str.startsWith("[")) {
          obj = json2ListRecursion(str, mapper);
        } else if (obj.toString().startsWith("{")) {
          obj = json2MapRecursion(str, mapper);
        }
      }
    }

    return list;
  }

  /**
   * 把 JSON 解析成 Map，如果 Map 内部的 Value 存在 jsonString，继续解析
   *
   * @throws Exception
   */
  private static Map<String, Object> json2MapRecursion(String json, ObjectMapper mapper)
      throws Exception {
    if (json == null) {
      return null;
    }

    Map<String, Object> map = mapper.readValue(json, Map.class);

    for (Map.Entry<String, Object> entry : map.entrySet()) {
      Object obj = entry.getValue();
      if (obj != null && obj instanceof String) {
        String str = ((String) obj);

        if (str.startsWith("[")) {
          List<?> list = json2ListRecursion(str, mapper);
          map.put(entry.getKey(), list);
        } else if (str.startsWith("{")) {
          Map<String, Object> mapRecursion = json2MapRecursion(str, mapper);
          map.put(entry.getKey(), mapRecursion);
        }
      }
    }

    return map;
  }

  /**
   * 将 JSON 数组转换为集合
   *
   * @throws Exception
   */
  public static <T> List<T> json2list(String jsonArrayStr, Class<T> clazz) throws Exception {
    JavaType javaType = getCollectionType(ArrayList.class, clazz);
    List<T> list = OBJECT_MAPPER.readValue(jsonArrayStr, javaType);
    return list;
  }

  /**
   * 获取泛型的 Collection Type
   *
   * @param collectionClass 泛型的Collection
   * @param elementClasses  元素类
   * @return JavaType Java类型
   * @since 1.0
   */
  public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
    return OBJECT_MAPPER.getTypeFactory().constructParametricType(collectionClass, elementClasses);
  }

  /**
   * 将 Map 转换为 JavaBean
   */
  public static <T> T map2pojo(T map, Class<T> clazz) {
    return OBJECT_MAPPER.convertValue(map, clazz);
  }

  /**
   * 将 Map 转换为 JSON
   */
  public static String mapToJson(Map map) {
    try {
      return OBJECT_MAPPER.writeValueAsString(map);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return "";
  }

  /**
   * 将 JSON 对象转换为 JavaBean
   */
  public static <T> T obj2pojo(Object obj, Class<T> clazz) {
    return OBJECT_MAPPER.convertValue(obj, clazz);
  }

  /**
   * 将指定节点的 JSON 数据转换为 JavaBean
   *
   * @throws Exception
   */
  public static <T> T json2pojoByTree(String jsonString, String treeNode, Class<T> clazz)
      throws Exception {
    JsonNode jsonNode = OBJECT_MAPPER.readTree(jsonString);
    JsonNode data = jsonNode.findPath(treeNode);
    return json2pojo(data.toString(), clazz);
  }

  /**
   * 将指定节点的 JSON 数组转换为集合
   *
   * @param jsonStr  JSON 字符串
   * @param treeNode 查找 JSON 中的节点
   * @throws Exception
   */
  public static <T> List<T> json2listByTree(String jsonStr, String treeNode, Class<T> clazz)
      throws Exception {
    JsonNode jsonNode = OBJECT_MAPPER.readTree(jsonStr);
    JsonNode data = jsonNode.findPath(treeNode);
    return json2list(data.toString(), clazz);
  }
}

